package com.yikome.monitor.service;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.yikome.monitor.entity.ERPMonitorADStatistics;
import com.yikome.monitor.entity.ERPMonitorEventStatistics;
import com.yikome.monitor.entity.ERPMonitorPageStatistics;
import com.yikome.monitor.entity.ERPMonitorProductStatistics;
import com.yikome.monitor.repository.ERPMonitorADStatisticsRepository;
import com.yikome.monitor.repository.ERPMonitorEventStatisticsRepository;
import com.yikome.monitor.repository.ERPMonitorPageStatisticsRepository;
import com.yikome.monitor.repository.ERPMonitorProductStatisticsRepository;
import com.yikome.monitor.request.CreateMonitorRequest;
import com.yikome.utils.JSONUtils;
import com.yikome.utils.SpecificationUtils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

@Service
public class MonitorUtService {
	 
	@Autowired
	private ERPMonitorEventStatisticsRepository monitorEventStatisticsRepository ; 
	@Autowired
	private ERPMonitorPageStatisticsRepository monitorPageStatisticsRepository ; 
	@Autowired
	private ERPMonitorProductStatisticsRepository monitorProductStatisticsRepository ; 
	@Autowired
	private ERPMonitorADStatisticsRepository monitorADStatisticsRepository ; 
	
	@SuppressWarnings("unchecked")
	@Async
	public void consumeMessage(String message) {
    	
    	CreateMonitorRequest monitorRequest = JSONUtils.parseJson(message , CreateMonitorRequest.class);
    	
    	if(StrUtil.equalsIgnoreCase(monitorRequest.getAimsCategory(), "PRODUCT")) {
    		
    		long productId = Long.parseLong(monitorRequest.getEventAims());
    		
    		String statisticsDate = DateUtil.formatDate(new DateTime(TimeZone.getTimeZone("GTM+8")));
    		
			Specification<ERPMonitorProductStatistics> specification = Specification.where(SpecificationUtils.equal("productId", productId))
    				.and(SpecificationUtils.equal("eventName", monitorRequest.getEventName()))
    				.and(SpecificationUtils.equal("statisticsDate", statisticsDate))
    				.and(SpecificationUtils.equal("hostName", monitorRequest.getHostName()));
    		ERPMonitorProductStatistics statistics = monitorProductStatisticsRepository.findOne(specification).orElse(null);
    		if(statistics == null) {
    			statistics = new ERPMonitorProductStatistics();
    			statistics.setCount(Integer.valueOf(1));
    			statistics.setEventName(monitorRequest.getEventName());
    			statistics.setProductId(productId);
    			statistics.setHostName(monitorRequest.getHostName());
    			statistics.setStatisticsDate(statisticsDate);
    		}else {
    			statistics.setCount(statistics.getCount() + 1);
    		}
    		monitorProductStatisticsRepository.save(statistics);
    		
    		
    	}else if(StrUtil.equalsIgnoreCase(monitorRequest.getAimsCategory(), "PAGE")) {
    		
    		String statisticsDate = DateUtil.formatDate(new DateTime(TimeZone.getTimeZone("GTM+8")));
    		
    		String eventAims = StrUtil.subBefore(monitorRequest.getEventAims(), "?", false);
    		Specification<ERPMonitorPageStatistics> specification = Specification.where(SpecificationUtils.equal("eventAims", eventAims))
    				.and(SpecificationUtils.equal("statisticsDate", statisticsDate));
    		ERPMonitorPageStatistics statistics = monitorPageStatisticsRepository.findOne(specification).orElse(null);
    		
    		if(statistics == null) {
    			statistics = new ERPMonitorPageStatistics();
    			statistics.setCount(Integer.valueOf(1));
    			statistics.setEventAims(eventAims);
    			statistics.setEventName("SHOW");
    			statistics.setStatisticsDate(statisticsDate);
    		}else {
    			statistics.setCount(statistics.getCount() + 1);
    		}
    		monitorPageStatisticsRepository.save(statistics);
    		
    	}else if(StrUtil.equalsIgnoreCase(monitorRequest.getAimsCategory(), "EVENT")) {
    		
    		String statisticsDate = DateUtil.formatDate(new DateTime(TimeZone.getTimeZone("GTM+8")));
    		
    		Specification<ERPMonitorEventStatistics> specification = Specification.where(SpecificationUtils.equal("eventAims", monitorRequest.getEventAims()))
    				.and(SpecificationUtils.equal("eventName", monitorRequest.getEventName()))
    				.and(SpecificationUtils.equal("statisticsDate", statisticsDate));
    		ERPMonitorEventStatistics statistics = monitorEventStatisticsRepository.findOne(specification).orElse(null);
    		
    		if(statistics == null) {
    			statistics = new ERPMonitorEventStatistics();
    			statistics.setCount(Integer.valueOf(1));
    			statistics.setEventAims(monitorRequest.getEventAims());
    			statistics.setEventName(monitorRequest.getEventName());
    			statistics.setStatisticsDate(statisticsDate);
    		}else {
    			statistics.setCount(statistics.getCount() + 1);
    		}
    		monitorEventStatisticsRepository.save(statistics);
    		
    		
    	}else if(StrUtil.equalsIgnoreCase(monitorRequest.getAimsCategory(), "AD")) {
    		
    		long adId = Long.parseLong(monitorRequest.getEventAims());
    		
    		String statisticsDate = DateUtil.formatDate(new DateTime(TimeZone.getTimeZone("GTM+8")));
    		
    		Specification<ERPMonitorADStatistics> specification = Specification.where(SpecificationUtils.equal("adId", adId))
    				.and(SpecificationUtils.equal("statisticsDate", statisticsDate));
    		
    		ERPMonitorADStatistics statistics = monitorADStatisticsRepository.findOne(specification).orElse(null);
    		
    		if(statistics == null) {
    			statistics = new ERPMonitorADStatistics();
    			statistics.setCount(Integer.valueOf(1));
    			statistics.setAdId(adId);
    			statistics.setEventName("SHOW");
    			statistics.setStatisticsDate(statisticsDate);
    		}else {
    			statistics.setCount(statistics.getCount() + 1);
    		}
    		monitorADStatisticsRepository.save(statistics);
    	}
    }
}